<template>
  <div class="tableSearchListFlexPage h100" style="padding-top: 12px;">
    <div>
      <a-form layout="vertical" >
        <a-row class="ecuBrief">
          <a-col :md="24" :sm="24">
            <span>
              {{ $t('VIN') }}：{{ EcuDetailInfo.vin || '-' }}
            </span>
            <span>
              {{ $t('车型') }}：{{ EcuDetailInfo.modelName || '-' }}
            </span>
            <span>
              {{ '车型代码' }}：{{ EcuDetailInfo.modelCode || '-' }}
            </span>
            <span>
              {{ '车辆版本' }}：{{ EcuDetailInfo.vehicleVersion || '-' }}
            </span>
            <span>
              {{ '更新日期' }}：{{ $options.filters.moment(EcuDetailInfo.createTime) || '-' }}
            </span>
          </a-col>
        </a-row>
        <a-row :gutter="[12,0]" type="flex" justify="space-around" align="middle">
          <!-- ECU名称 -->
          <a-col :md="5" :sm="24">
            <a-form-model-item :label="$t('ECU名称')">
              <ecu-name-select
                v-model="queryParam.ecuName"
                @change="refreshData(true)"
              ></ecu-name-select>
            </a-form-model-item>
          </a-col>
          <!-- ECUID -->
          <a-col :md="5" :sm="24">
            <a-form-model-item :label="$t('ECUID')">
              <ecu-id-select
                v-model="queryParam.ecuId"
                @change="refreshData(true)"
              ></ecu-id-select>
            </a-form-model-item>
          </a-col>
          <!-- ECU软件编码 -->
          <a-col :md="5" :sm="24">
            <a-form-model-item :label="$t('ECU软件编码')">
              <a-select
                v-model="queryParam.ecuSoftCode"
                @change="refreshData(true)"
                showSearch
                allow-clear
                optionFilterProp="children"
                :filterOption="false"
                @search="loadSoftWareCodes"
                :placeholder="$t('pleaseSelect') + $t('softwareCoding')"
                style="width: 100%"
              >
                <a-select-option
                  v-for="(item, index) in filterEcuList"
                  :key="index"
                  :value="item"
                  :title="item"
                >{{ item }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <!-- 软件版本 -->
          <a-col :md="5" :sm="24" style="margin-top: -3px;">
            <a-form-model-item>
              <template #label>
                <span>{{ $t('软件版本') }}</span>
                <question-mark-tip
                  :tip-title="$t('需先输入ECUID,软件版本才能进行搜索')"
                  :popupContainer="getPopupContainer">
                </question-mark-tip>
              </template>
              <software-version-select
                mode="default"
                :query="{ecuModuleId:queryParam.ecuId}"
                :time="time"
                :value="queryParam.ecuVersion"
                @changeoption="(val) => {
                  queryParam.ecuVersion = val; refreshData(true)}"
              >
              </software-version-select>
            </a-form-model-item>
          </a-col>
          <!-- 硬件版本 -->
          <a-col :md="4" :sm="24">
            <a-form-model-item :label="$t('硬件版本')">
              <a-input
                v-model="queryParam.hardVersion"
                :placeholder="$t('pleaseInput') + '硬件版本'"
                allow-clear
                @keyup.enter="refreshData(true)"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="[16,0]" type="flex" justify="end">
          <a-col :md="6" :sm="24" style="margin-bottom: 8px;">
            <div class="searchBtns" style="float: right;">
              <a-button-group>
                <auth-button type="primary" @click="refreshData(true)">
                  <i class="iconfont icon-chazhao"></i>
                  {{ $t('inquiry') }}
                </auth-button>
                <a-button @click="handleClear">
                  <i class="iconfont icon-qingkong"></i>
                  {{ $t('clear') }}
                </a-button>
              </a-button-group>
              <auth-button type="primary" @click="exportXlsx" :loading="exporting">
                {{ $t('导出') }}
              </auth-button>
            </div>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="commonTableSty">
      <s-table
        class="h100"
        ref="table"
        :columns="columns"
        :data="loadData"
        :scrollX="900"
        :rowKey="(record, index) => index"
        size="default" >
        <template slot-scope="text" v-for="column in commonColumns" :slot="column.scopedSlots.customRender">
          <table-ellipsis :text="text || '-'" :key="column.key"></table-ellipsis>
        </template>
        <!-- 软件版本 -->
        <template slot="ecuVersion" slot-scope="text, record">
          <span v-if=" text && record.changedItem && isnumber(record.changedItem, '1')" style="font-weight:bold">{{ text }}</span>
          <question-mark-tip
            v-if="text && record.changedItem && isnumber(record.changedItem, '1')"
            :tip-title="$t('与上次发生变化')"
            :popupContainer="getPopupContainer"
          ></question-mark-tip>
          <span v-else>{{ text || '-' }}</span>
          <span v-if="text==='7F22'">{{ '(消极应答)' }}</span>
        </template>
        <!-- 软件编码(车端上报) -->
        <template slot="softwareCode" slot-scope="text, record">
          <span v-if=" text && record.changedItem && isnumber(record.changedItem, '2')" style="font-weight:bold">{{ text }}</span>
          <question-mark-tip
            v-if="text && record.changedItem && isnumber(record.changedItem, '2')"
            :tip-title="$t('与上次发生变化')"
            :popupContainer="getPopupContainer"
          ></question-mark-tip>
          <span v-else>{{ text || '-' }}</span>
          <span v-if="text==='7F22'">{{ '(消极应答)' }}</span>
        </template>
        <!-- 硬件版本 -->
        <template slot="hardVersion" slot-scope="text, record">
          <span v-if=" text && record.changedItem && isnumber(record.changedItem, '3')" style="font-weight:bold">{{ text }}</span>
          <question-mark-tip
            v-if="text && record.changedItem && isnumber(record.changedItem, '3')"
            :tip-title="$t('与上次发生变化')"
            :popupContainer="getPopupContainer"
          ></question-mark-tip>
          <span v-else>{{ text || '-' }}</span>
          <span v-if="text==='7F22'">{{ '(消极应答)' }}</span>
        </template>
      </s-table>
    </div>
  </div>
</template>
<script>
import { STable } from '@/components'
import moment from 'moment'
import AuthButton from '@/components/AuthButton'
import { EcuVersionListByVehicleVersion } from '@/views/vehicle/config/list'
import CustomModal from '@/components/CustomModal/index.vue'
import UpgradeLogsPopUps from '@/views/vehicle/components/UpgradeLogsPopUps.vue'
import { downloadBlobFile } from '@/utils/functions'
import EcuNameSelect from '@/views/components/EcuNameSelect.vue'
import EcuIdSelect from '@/views/components/EcuIdSelect.vue'
import SoftwareVersionSelect from '@/views/components/SoftwareVersionSelect.vue'
import TableEllipsis from '@/components/Ellipsis/TableEllipsis'
import QuestionMarkTip from '@/views/components/QuestionMarkTip'
const initQueryParam = {
  ecuId: undefined,
  ecuName: undefined,
  ecuSoftCode: undefined,
  ecuVersion: undefined,
  hardVersion: undefined
}
export default {
  components: {
    STable,
    AuthButton,
    CustomModal,
    UpgradeLogsPopUps,
    EcuNameSelect,
    EcuIdSelect,
    SoftwareVersionSelect,
    TableEllipsis,
    QuestionMarkTip
  },
  props: {
    traceId: {
      type: String,
      default: ''
    },
    EcuDetailInfo: {
      type: Object,
      default () {
        return { }
      }
    }
  },
  computed: {
    commonColumns () {
      return this.columns.filter(item => !item.special)
    }
  },
  created () {
    this.loadSoftWareCodes('')
  },
  data () {
    return {
      moment,
      columns: [...EcuVersionListByVehicleVersion],
      // 列表数据
      tableData: [],
      queryParam: { ...initQueryParam },
      upgradeLogsVisibal: false,
      exporting: false,
      // 软件编码下拉框数据
      filterEcuList: [],
      time: Date.now()
    }
  },
  methods: {
    getPopupContainer () {
      return document.body
    },
    isnumber (val, isVal) {
      return val.indexOf(isVal)!=-1
    },
    // 软件编码下拉数据
    loadSoftWareCodes (val) {
      this.$api.getEcuList({ softwareCode: val, pageSize: 100 }).then(res => {
        // 软件编码去重
        this.filterEcuList = new Set([...res.data].map(ecu => ecu.softwareCode))
      })
    },
    // 导出
    exportXlsx () {
      this.exporting = true
      this.$api.exportVehicleVersionSpecificEcuVersion({ ...this.queryParam, traceId: this.traceId }).then((res) => {
        downloadBlobFile(res, '车辆版本具体ecu版本' + moment().format('YYYYMMDDHHmmss') + '.xlsx')
      }).finally(() => {
        this.exporting = false
      })
    },
    // 重置
    handleClear () {
      this.queryParam = { ...initQueryParam }
      this.refreshData(true)
    },
    // 刷新
    refreshData (isToFirstPage = false) {
      const vm = this
      vm.$nextTick(() => {
        this.$refs.table.refresh(isToFirstPage)
      })
    },
    loadData (parameter) {
      const vm = this
      // 如果是全部则不传值给后端
      const JnqueryParam = JSON.parse(JSON.stringify(vm.queryParam))
      // eslint-disable-next-line no-restricted-syntax
      for (const key in JnqueryParam) {
        if (JnqueryParam[key] === '') {
          JnqueryParam[key] = undefined
        }
      }
      this.tableData = []
      const apiPromise = vm.$api.getEcuVersionListByVehicleVersion({ ...parameter, ...JnqueryParam, traceId: this.traceId })
      return new Promise((resolve, reject) => {
        return apiPromise.then(res => {
          if (res.data) {
            this.tableData = res.data
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    resize() {
      this.$refs.table.getDomHeight();
    }
  }
}
</script>
<style scoped lang="less">
.ecuBrief {
  margin-bottom: 10px;
  span{
  font-size: 16px !important;
  margin-right: 40px;
  }
}
 ::v-deep .ant-form-item-label {
  padding-bottom: 2px;
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
